<?php


class ProductAction extends SecurityAction{
	function _initialize(){
        $this->assign("menuFirst",session('menuFirst')); 
        $this->assign("menuSecond",session('menuSecond')); 
    }
    
	public $uploadPath = '/files/product/';
	function plist(){

	    $od = M("yq_dict");
	    $tmp01 = $od->where(array('type'=>'product'))->select();
	    foreach($tmp01 as $key=>$value){
	       $class[$value['code']] = $value; 
	    }
	    $this->assign('class',$class);

		$ob = M("yq_product");
        
		$sql = array();
		if($_GET['pname']){
		    $sql[] = array('pname'=>array('like',"%{$_GET['pname']}%"));
		}
		
		$cpp = 20; //分页数
        import('ORG.Util.Page');// 导入分页类
        $count      = $ob->where($sql)->count();// 查询满足要求的总记录数
        $Page       = new Page($count,$cpp);// 实例化分页类 传入总记录数和每页显示的记录数
        $show       = $Page->show();// 分页显示输出
        
		$list = $ob->where($sql)->limit($Page->firstRow.','.$Page->listRows)
					->order(array('tid'=>'desc'))->select();

		$this->assign("list", $list);
		$this->assign('show',$show);// 赋值分页输出

		$this->display("Product/plist");
	}
	

	function detail(){
		if ($_GET['opt'] == 'add'){  //新增跳转
			$ob = M("yq_dict");
			$list = $ob->where(array("type"=>"product"))->select();

			$this->assign("list", $list);
			$this->display("Product/detail");
			return;
		}

	    if ($_GET["opt"] == 'edit'){   //编辑
	        $ob = M("yq_product");
	        $info = $ob->where(array("tid"=>$_GET["tid"]))->find();

	        $ob = M("yq_dict");
			$list = $ob->where(array("type"=>"product"))->select();

			$this->assign("list", $list);
	        $this->assign("modify", '1');
	        $this->assign("info", $info);

	        $this->display('Product/detail');
	        return;
	    }
	    
	    if($_POST){
	    	// var_dump($_FILES);
	    	// var_dump($_FILES['Avatar']);
	        //添加
			if (isset($_FILES) && isset($_FILES['Avatar'])){
			    if (!is_dir(APP_PATH.'/files/')){
			        mkdir(APP_PATH.'/files/',0755,true);
			    }
			 	if (!is_dir(APP_PATH.'/files/product/')){
			        mkdir(APP_PATH.'/files/product/',0755,true);
			    }
			     //文件上传配置
		        import('ORG.Net.UploadFile');
		        $upload = new UploadFile();// 实例化上传类
		        $upload->maxSize  = 2000000 ;// 设置附件上传大小;默认为-1，不限制上传大小
		        $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		        $upload->allowTypes = array('image/jpg','image/jpeg','image/pjpeg','image/png','image/gif');
		        $upload->savePath = APP_PATH.$this->uploadPath;// 设置附件上传目录
		        $upload->saveRule = 'uniqid';
		        $upload->uploadReplace = true;
		        //开始上传
		        if(!$upload->upload()){
// 		            $this->error($upload->getErrorMsg());
		        }else{
		            $info =  $upload->getUploadFileInfo();
		            $_POST['imgurl'] = $this->uploadPath . $info[0]['savename'];
		        }
		        
			}
	        $ob = M("yq_product");

	        $_POST['adminid'] = session('info.tid');
	        $_POST['add_time'] = time();//date('Y-m-d H:i:s', time());
	    
	        if($_POST['is_forbit']=='-1'){
	        	$ob->where(array('tid'=>$_POST['tid']))->delete();
	        	$this->redirect('/Product/plist/');
	        	return;
	        }
	        
	        if($_POST['tid'] != ''){
	            $ob->where(array('tid'=>$_POST['tid']))->save($_POST);
	        }else{
	            $ob->add($_POST);
	        }
	        	
	        $this->redirect('/Product/plist/');
	        return;
	    }
	}


}

?>